﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using VideoClassLibrary;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using VideoClassLibrary.Model;
namespace VideoClassLibrary.BusinessLogicLayer
{
    /// <summary>
   /// 管理员业务逻辑模型
  /// </summary>
     
    public class AdminModule
    {
          /// <summary>
        /// 判断是否可以添加
       /// </summary>
        public bool IsName(string name)
        {
            string sql = "select count(*) from V_adminUser where VA_loginname=@name ";
            SqlParameter username = new SqlParameter("@name", name);
            VDataBase vb = new VDataBase();
            object result = vb.GetDataByScalar(sql, false, username);
            int n = Convert.ToInt32(result);
            if (n != 0)
            {
                return false;

            }
            else
                return true;

        }
     
          /// <summary>
        ///  管理员登陆 如果返回 0则为运行管理员，1为学校管理员，-1则是不能登陆
       /// </summary>
        public int Admin(string name, string pwd)
        {
            #region will be remove,modify by chenfeng. 
            //V_adminUser ad = new V_adminUser();
            //string sql = "select VA_type from V_adminUser where VA_loginname=@name and VA_password=@Pwd";
            //SqlParameter p1 = new SqlParameter("@Name", name);
            //SqlParameter p2 = new SqlParameter("@Pwd", pwd);
            //VDataBase vb = new VDataBase();
            //SqlDataReader read = vb.GetDataByReader(sql, false, p1, p2);
            //if (read.HasRows)
            //{
            //    read.Read();
            //    int i = read.GetInt32(0);

            //    if (i == 0)
            //    {
            //        return 0;
            //    }
            //    else
            //    { return 1; }

            //}

            //else
            //{
            //    return -1;
            //}
            #endregion

            return this.Admin(name, pwd, 1);
        }

        public int Admin(string name, string pwd,int isEnable)
        {
            V_adminUser ad = new V_adminUser();
            string sql = "select VA_type from V_adminUser where VA_loginname=@name and VA_password=@Pwd and va_isenable = @VA_isenable";
            SqlParameter p1 = new SqlParameter("@Name", name);
            SqlParameter p2 = new SqlParameter("@Pwd", pwd);
            SqlParameter p3 = new SqlParameter("@VA_isenable", isEnable);
            VDataBase vb = new VDataBase();
            SqlDataReader read = vb.GetDataByReader(sql, false, p1, p2,p3);
            if (read.HasRows)
            {
                read.Read();
                int i = read.GetInt32(0);

                if (i == 0)
                {
                    return 0;
                }
                else
                { return 1; }

            }

            else
            {
                return -1;
            }
        }

        /// <summary>
        ///  根据ID管理员实体
        /// </summary>
        /// <returns></returns>
        public V_adminUser GetAdminModelById(int AdId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ID,Create_time ,Update_time,VA_loginname,VA_password,VA_realname ,VA_mobile,VA_email ,VA_type,SchoolID,Va_IsEnable FROM V_adminUser ");
            strSql.Append(" where ID=@ID ");
            Database db = VDataBase.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(command, "ID", DbType.Int32, AdId);
            DataSet dsResult = db.ExecuteDataSet(command);
            V_adminUser model = null;

            if (dsResult != null && dsResult.Tables.Count > 0 && dsResult.Tables[0].Rows.Count > 0)
            {
                DataRow dr = dsResult.Tables[0].Rows[0];
                model = new V_adminUser();
                model.Create_time = dr["Create_time"] == DBNull.Value ? System.DateTime.Now : System.DateTime.Parse(dr["create_time"].ToString());
                model.ID = int.Parse(dr["ID"].ToString());
                model.VA_loginname = dr["VA_loginname"].ToString();
                model.VA_password = dr["VA_password"].ToString();
                model.VA_realname = dr["VA_realname"].ToString();
                model.VA_type = int.Parse(dr["VA_type"].ToString());
                model.VA_mobile = dr["VA_mobile"].ToString();
                model.VA_email = dr["VA_email"].ToString();
                model.SchoolID = int.Parse(dr["SchoolID"].ToString());
                model.Va_IsEnable = int.Parse(dr["Va_IsEnable"].ToString());
                dsResult = null;
            }
            return model;
        }
          /// <summary>
        /// 管理员登陆并且可以记录管理员信息
       /// </summary> 
        public V_adminUser AdminLogin(string name,string pwd)
        {  
            V_adminUser admin = new V_adminUser();
            string sql = "SELECT ID,Create_time ,Update_time,VA_loginname,VA_password,VA_realname ,VA_mobile,VA_email ,VA_type,SchoolID,VA_IsEnable FROM V_adminUser where VA_loginname=@Aname and VA_password=@Apwd";
            SqlParameter par = new SqlParameter("@Aname", name);
            SqlParameter par1 = new SqlParameter("@Apwd", pwd);
            VDataBase vb = new VDataBase();
            SqlDataReader read = vb.GetDataByReader(sql, false,par,par1);
            if (read.HasRows)
            {
                read.Read();
                admin.ID = Convert.ToInt32(read[0]);
                admin.Create_time = Convert.ToDateTime(read[1]);
                admin.Update_time = Convert.ToDateTime(read[2]);
                admin.VA_loginname = read[3].ToString();
                admin.VA_password = read[4].ToString();
                admin.VA_realname = read[5].ToString();
                admin.VA_mobile = read[6].ToString();
                admin.VA_email = read[7].ToString();
                admin.VA_type = Convert.ToInt32(read[8].ToString());
                admin.SchoolID = Convert.ToInt32(read[9].ToString());
                admin.Va_IsEnable = Convert.ToInt32(read[10].ToString());
                read.Close();
                return admin;
            }
            else
            {
                return null;
            }
           
        }
      
         /// <summary>
        /// 获取管理员所有信息
       /// </summary>  
        public DataSet GetAllAdmin()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ID,Create_time ,Update_time,VA_loginname,VA_password,VA_realname ,VA_mobile,VA_email ,VA_type,SchoolID,VA_IsEnable ");
            strSql.Append(" FROM V_adminUser where 1=1 order by Create_time desc ");
            Database db = VDataBase.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        public List<V_adminUser> GetAllAdminList()
        {
            List<V_adminUser> list = new List<V_adminUser>();
            string sql = "SELECT ID,Create_time ,Update_time,VA_loginname,VA_password,VA_realname ,VA_mobile,VA_email ,VA_type,SchoolID,VA_IsEnable FROM V_adminUser order by Create_time desc";
            VDataBase vb = new VDataBase();
            SqlDataReader read = vb.GetDataByReader(sql, false);
            if(read.HasRows)
            {
                while (read.Read())
                {
                    V_adminUser admin=new V_adminUser();
                    admin.ID=Convert.ToInt32(read[0]);
                    admin.Create_time=Convert.ToDateTime(read[1]);
                    admin.Update_time=Convert.ToDateTime(read[2]);
                    admin.VA_loginname=read[3].ToString();
                    admin.VA_password=read[4].ToString();
                    admin.VA_realname=read[5].ToString();
                    admin.VA_mobile=read[6].ToString();
                    admin.VA_email=read[7].ToString();
                    admin.VA_type=Convert.ToInt32(read[8].ToString());
                    admin.SchoolID=Convert.ToInt32(read[9].ToString());
                    admin.Va_IsEnable = Convert.ToInt32(read[10].ToString());
                    list.Add(admin);
                }
            }
            read.Close();
            return list;
        }
      
        /// <summary>
        /// 按姓名查找管理员
       /// </summary>  

         public List<V_adminUser> GetAllAdmin(string name)
        {
            List<V_adminUser> list = new List<V_adminUser>();
            string sql = "SELECT ID,Create_time ,Update_time,VA_loginname,VA_password,VA_realname ,VA_mobile,VA_email ,VA_type,SchoolID,VA_IsEnable FROM V_adminUser where VA_loginname=@name order by Create_time desc ";
            SqlParameter par=new SqlParameter("@name",name);
            VDataBase vb = new VDataBase();
            SqlDataReader read = vb.GetDataByReader(sql, false,par);
            if(read.HasRows)
            {
                while (read.Read())
                {
                    V_adminUser admin=new V_adminUser();
                    admin.ID=Convert.ToInt32(read[0]);
                    admin.Create_time=Convert.ToDateTime(read[1]);
                    admin.Update_time=Convert.ToDateTime(read[2]);
                    admin.VA_loginname=read[3].ToString();
                    admin.VA_password=read[4].ToString();
                    admin.VA_realname=read[5].ToString();
                    admin.VA_mobile=read[6].ToString();
                    admin.VA_email=read[7].ToString();
                    admin.VA_type=Convert.ToInt32(read[8].ToString());
                    admin.SchoolID=Convert.ToInt32(read[9].ToString());
                    admin.Va_IsEnable = Convert.ToInt32(read[10].ToString());
                    list.Add(admin);
                }
            }
            read.Close();
            return list;
        }
       /// <summary>
        /// 添加管理员
       /// </summary>  
         public int AddAdmin(V_adminUser admin)
         {
             VDataBase v = new VDataBase();
             string sql = "insert into V_adminUser values (@Create_time,@Updatete_time,@LoginName,@pwd,@realName,@mobile,@email,@VA_Type,@School,@VA_IsEnable)";
             SqlParameter p1=new SqlParameter("@Create_time",admin.Create_time);
             SqlParameter p2=new SqlParameter("@Updatete_time",admin.Update_time);
             SqlParameter p3=new SqlParameter("@LoginName",admin.VA_loginname);
             SqlParameter p4=new SqlParameter("@pwd",admin.VA_password);
             SqlParameter p5=new SqlParameter("@realName",admin.VA_realname);
             SqlParameter p6=new SqlParameter("@mobile",admin.VA_mobile);
             SqlParameter p7=new SqlParameter("@email",admin.VA_email);
            SqlParameter p8=new SqlParameter("@VA_Type",admin.VA_type);
              SqlParameter p9=new SqlParameter("@School",admin.SchoolID);
              SqlParameter p10 = new SqlParameter("@VA_IsEnable", admin.Va_IsEnable);
              int i = v.GetDataByNonQuery(sql, false, p1, p2, p3, p4, p5, p6, p7, p8, p9,p10);
              if (i > 0)
              { return 1; }
              else
              { return -1; }

         }
         /// <summary>
         /// MD5加密
         /// </summary>
         /// <returns></returns>
       
         public static string MD5Enctrypt(string pwd)
         {
             return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5");
         }
        /// <summary>
         ///修改管理员密码
         /// </summary>
         /// <returns></returns>
         public int UpdatePwd(string newpwd,int id)
         {

             VDataBase v = new VDataBase();
             string sql = "update V_adminUser set Update_time=GETDATE(),VA_password=@pwd where ID=@id";
             SqlParameter p1 = new SqlParameter("@pwd", MD5Enctrypt(newpwd));
             SqlParameter p2 = new SqlParameter("@id", id);
             int i = v.GetDataByNonQuery(sql, false, p1, p2);
             if(i>0)
             { return 1; }
             else
             { return -1; }
         }

         /// <summary>
         /// 根据ID修改管理员信息
         /// </summary>
         /// <returns></returns> 
         public int UpdateAdminUser(V_adminUser user)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append(" update V_adminUser set  Update_time=@Update_time,SchoolID=@SchoolID,VA_loginname=@VA_loginname,");
             if (user.VA_password == null || user.VA_password == "")
             {
                 strSql.Append("VA_realname=@VA_realname,VA_mobile=@VA_mobile,VA_email=@VA_email,VA_type=@VA_type,VA_IsEnable=@VA_IsEnable where  ID=@id");
             }
             else
             {
                 strSql.Append(" VA_password=@VA_password,VA_realname=@VA_realname,VA_mobile=@VA_mobile,VA_email=@VA_email ,VA_type=@VA_type,VA_IsEnable=@VA_IsEnable where  ID=@id");
             }

             Database db = VDataBase.CreateDatabase();
             DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
             db.AddInParameter(dbCommand, "id", DbType.Int32, user.ID);
             db.AddInParameter(dbCommand, "Update_time", DbType.DateTime, user.Update_time);
             db.AddInParameter(dbCommand, "VA_loginname", DbType.AnsiString, user.VA_loginname);
             if (user.VA_password == null || user.VA_password == "")
             { }
             else { db.AddInParameter(dbCommand, "VA_password", DbType.AnsiString, MD5Enctrypt(user.VA_password)); }

             db.AddInParameter(dbCommand, "VA_realname", DbType.AnsiString, user.VA_realname);
             db.AddInParameter(dbCommand, "VA_mobile", DbType.AnsiString, user.VA_mobile);
             db.AddInParameter(dbCommand, "VA_email", DbType.AnsiString, user.VA_email);
             db.AddInParameter(dbCommand, "VA_type", DbType.AnsiString, user.VA_type);
             db.AddInParameter(dbCommand, "SchoolID", DbType.Int32, user.SchoolID);
             db.AddInParameter(dbCommand, "VA_IsEnable", DbType.Int32, user.Va_IsEnable);
             int result = db.ExecuteNonQuery(dbCommand);
             if (result > 0)
             {
                 return 1;
             }
             else
             {
                 return -1;
             }
         }

         /// <summary>
         /// 查询本学校的管理员
         /// </summary>  
         public DataSet GetAdmin(int schoolid)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("SELECT ID,Create_time ,Update_time,VA_loginname,VA_password,VA_realname ,VA_mobile,VA_email ,VA_type,SchoolID ,VA_IsEnable");
             strSql.Append(" FROM V_adminUser ");
             if (schoolid != -1)
             {
                 strSql.Append("where schoolid=" + schoolid);
             }
             Database db = VDataBase.CreateDatabase();
             return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
         }
         /// <summary>
         /// 根据条件查询
         /// </summary>  
         public DataSet GetAdmin(int schoolid,string strwhere)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("SELECT ID,Create_time ,Update_time,VA_loginname,VA_password,VA_realname ,VA_mobile,VA_email ,VA_type,SchoolID ,VA_IsEnable");
             strSql.Append(" FROM V_adminUser where VA_type="+strwhere);
             if (schoolid != -1)
             {
                 strSql.Append( "and schoolid="+schoolid);
             }
             Database db = VDataBase.CreateDatabase();
             return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
         }
    }
}
